package com.cp.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cp.common.util.LendNoUtils;
import com.cp.core.enums.LendStatusEnum;
import com.cp.core.mapper.LendMapper;
import com.cp.core.pojo.entity.BorrowInfo;
import com.cp.core.pojo.entity.Borrower;
import com.cp.core.pojo.entity.Lend;
import com.cp.core.pojo.vo.BorrowInfoApprovalVO;
import com.cp.core.pojo.vo.BorrowerDetailVO;
import com.cp.core.service.BorrowerService;
import com.cp.core.service.DictService;
import com.cp.core.service.LendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标的准备表 服务实现类
 * </p>
 *
 * @author ming
 * @since 2022-07-27
 */
@Service
public class LendServiceImpl extends ServiceImpl<LendMapper, Lend> implements LendService {
    @Resource
    private DictService dictService;
    @Resource
    private BorrowerService borrowerService;
    @Override
    public void createLend(BorrowInfoApprovalVO borrowInfoApprovalVO, BorrowInfo borrowInfo) {
        Lend lend = new Lend();
        lend.setUserId(borrowInfo.getUserId());
        lend.setBorrowInfoId(borrowInfo.getId());
        lend.setLendNo(LendNoUtils.getLendNo());
        lend.setTitle(borrowInfoApprovalVO.getTitle());
        lend.setAmount(borrowInfo.getAmount());
        lend.setPeriod(borrowInfo.getPeriod());
        lend.setLendYearRate(borrowInfoApprovalVO.getLendYearRate().divide(new BigDecimal(100)));
        lend.setServiceRate(borrowInfoApprovalVO.getServiceRate().divide(new BigDecimal(100)));
        lend.setReturnMethod(borrowInfo.getReturnMethod());
        lend.setLowestAmount(new BigDecimal("100"));  //最低投资金额
        lend.setInvestAmount(new BigDecimal("0"));    //已投金额
        lend.setInvestNum(0);  //已投人数
        lend.setPublishDate(LocalDateTime.now());
        LocalDate localDate = LocalDate.parse(borrowInfoApprovalVO.getLendStartDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        lend.setLendStartDate(localDate);
        LocalDate lendEndDate = localDate.plusMonths(borrowInfo.getPeriod());
        lend.setLendEndDate(lendEndDate);
        lend.setLendInfo(borrowInfoApprovalVO.getLendInfo());
         //平台预期收益 = 年化/12*期数
        BigDecimal monthRate = lend.getServiceRate().divide(new BigDecimal(12), 8, BigDecimal.ROUND_UP);
        BigDecimal expectAmount = lend.getAmount().multiply(monthRate.multiply(new BigDecimal(lend.getPeriod())));
        lend.setExpectAmount(expectAmount);
        lend.setRealAmount(new BigDecimal(0));
        lend.setStatus(LendStatusEnum.INVEST_RUN.getStatus());
        lend.setCheckTime(LocalDateTime.now());
        //审核人
        lend.setCheckAdminId(1L);
        this.baseMapper.insert(lend);
    }

    @Override
    public List<Lend> selectList() {
        List<Lend> lendList = baseMapper.selectList(null);
        lendList.forEach(lend -> {
            String returnMethod = dictService.getNameByDictCodeAndValue("returnMethod", lend.getReturnMethod());
            String status = LendStatusEnum.getMsgByStatus(lend.getStatus());
            lend.getParam().put("returnMethod", returnMethod);
            lend.getParam().put("status", status);
        });
        return lendList;
    }

    @Override
    public Map<String, Object> getLendDetail(Long id) {
        Lend lend = this.baseMapper.selectById(id);
        String returnMethod = dictService.getNameByDictCodeAndValue("returnMethod", lend.getReturnMethod());
        String status = LendStatusEnum.getMsgByStatus(lend.getStatus());
        lend.getParam().put("returnMethod", returnMethod);
        lend.getParam().put("status", status);
        Borrower borrower = borrowerService.getOne(new QueryWrapper<Borrower>().eq("user_id", lend.getUserId()));
        BorrowerDetailVO borrowerDetail = borrowerService.getBorrowerDetailVoById(borrower.getId());
        Map<String, Object> result  =  new HashMap<>();
        result.put("lend",lend);
        result.put("borrower",borrowerDetail);
        return result;
    }
}
